package newcoder;

import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.FutureTask;
import java.util.stream.Stream;

public class NC93 {
    /**
     * lru design
     * @param operators int整型二维数组 the ops
     * @param k int整型 the k
     * @return int整型一维数组
     */
    public int[] LRU (int[][] operators, int k) {
        // write code here
        
        LinkedList<int[]> res = new LinkedList<>(); //长度为k的队列
        List<Integer> target = new ArrayList<>();//return
        Map<Integer,int[]> map = new HashMap<>();
        for(int i =0;i<operators.length;++i){
            if(operators[i].length==3){ //set
                if(map.containsKey(operators[i][1])){
                    int[] temp = map.remove(operators[i][1]);
                    res.remove(temp);
                    res.addLast(operators[i]);
                    map.put(operators[i][1],operators[i]);
                }else if(res.size()==k){
                    int [] temp = res.removeFirst();
                    map.remove(temp[1]);
                    res.addLast(operators[i]);
                    map.put(operators[i][1],operators[i]);
                }else{
                    res.addLast(operators[i]);
                    map.put(operators[i][1],operators[i]);
                }
            }
            if(operators[i].length==2){//get
                int[] t = map.get(operators[i][1]);
                if (res.contains(t)){
                    target.add(t[2]);
                    res.remove(t);
                    res.addLast(t);
                }
                else target.add(-1);
            }
        }

        int[] tar = new int[target.size()];
        for (int i=0;i<tar.length;i++){
            tar[i] = target.get(i);
        }
        return tar;
    }

    public static void main(String[] args) {
        NC93 nc = new NC93();
        int[] c = nc.LRU(new int[][]{{1,1,1},{1,1,2},{2,1}},2);
        for (int i:c){
            System.out.println(i);
        }
//        String address = "门前大桥下游过一群鸭";
//        List<String> list= new ArrayList<>();
//        list.add("一群鸭");
//        list.add("大桥下");
//        list.add("门前");
//        list.add("游过");
//        list.add("我家");
//        list.stream().sorted(Comparator.comparing(a->address.indexOf(a))).forEach(System.out :: println);

    }
}
